<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="good" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="addChild('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list" default-expand-all :props="{children:'children', label:'manager'}">
          <!-- 使用作用域插槽获取组件内部的数据 -->
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="good" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addChild(data.id)">添加子部门
                        </el-dropdown-item>
                        <el-dropdown-item @click.native="editChild(data.id)">编辑子部门
                        </el-dropdown-item>
                        <!-- 删除功能优化 -->
                        <el-dropdown-item v-if="data.children.length===0" @click.native="delChild(data.id)">删除子部门
                        </el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog :title=" isEdit ? '编辑' : '添加'" :visible.sync="showDialog" :close-on-click-modal="false" :close-on-press-escape="false">
        <!-- v-if="showDialog" 解决数据不更新 -->
        <dept-dialog v-if="showDialog" :id="curId" :is-edit="isEdit" :origin-list="originList" @success="hSuccess" @close="showDialog=false" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getDepartment, delDepartmentById } from '@/api/department'
import { transDataToTreeData } from '@/utils/index'
import deptDialog from './deptDialog.vue'
export default {
  components: { deptDialog },
  data() {
    return {
      list: [], // 保存部门列表数据
      showDialog: false,
      curId: '', // 当前点击部门的id
      isEdit: false
    }
  },
  created() {
    this.loadDepartment()
  },
  methods: {
    // 收到子组件中的success事件
    hSuccess() {
      // 关闭弹层，再次更新数据
      this.showDialog = false
      this.loadDepartment()
    },
    // 加载企业部门列表
    async loadDepartment() {
      try {
        const res = await getDepartment()
        this.originList = res.data.depts.map(item => { return { id: item.id, name: item.name, code: item.code, pid: item.pid } })
        console.log(res, 'getDepartment')
        res.data.depts.shift() // 删除第一条数据:传智播客
        // this.list = res.data.depts
        // 实现将平铺数据转换成树形数据
        this.list = transDataToTreeData(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    },
    // 添加部门
    addChild(id) {
      this.curId = id
      this.showDialog = true
      this.isEdit = false
    },
    // 编辑部门
    editChild(id) {
      this.curId = id
      this.showDialog = true
      this.isEdit = true
    },
    // 删除
    delChild(id) {
      this.$confirm('确认要删除吗?').then(
        async() => {
          const res = await delDepartmentById(id)
          if (res.code !== 10000) return this.$message.error(res.message)
          this.$message.success(res.message)
          // 重新渲染页面数据
          this.loadDepartment()
        }
      ).catch(err => err)
    }

  }
}
</script>
